package com.kotlin.book.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.book.databinding.ItemHomeSwiperBinding
import com.kotlin.book.entry.Swiper
import com.kotlin.book.util.Util

class HomeSwiperItemAdapter(private var itemList: List<Swiper>) :
    RecyclerView.Adapter<HomeSwiperItemAdapter.ItemViewHolder>() {
    class ItemViewHolder(itemBinding: ItemHomeSwiperBinding) :
        RecyclerView.ViewHolder(itemBinding.root) {
        val image: ImageView = itemBinding.mainSwiperImage
        val title: TextView = itemBinding.mainSwiperTitle
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding =
            ItemHomeSwiperBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return itemList.size
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = itemList[position]
        holder.title.text = item.type

        Util.getImage(item.url, holder.image, holder.itemView.context)
    }
}